package client.gui;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.Map;

import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.ListModel;
import javax.swing.ListSelectionModel;
import javax.swing.WindowConstants;

import client.domein.ChatController;
import client.domein.ChatListener;

/**
* This code was edited or generated using CloudGarden's Jigloo
* SWT/Swing GUI Builder, which is free for non-commercial
* use. If Jigloo is being used commercially (ie, by a corporation,
* company or business for any purpose whatever) then you
* should purchase a license for each developer using Jigloo.
* Please visit www.cloudgarden.com for details.
* Use of Jigloo implies acceptance of these licensing terms.
* A COMMERCIAL LICENSE HAS NOT BEEN PURCHASED FOR
* THIS MACHINE, SO JIGLOO OR THIS CODE CANNOT BE USED
* LEGALLY FOR ANY CORPORATE OR COMMERCIAL PURPOSE.
*/
public class LobbyFrame extends javax.swing.JFrame implements ChatListener, NameListener
{
	private static final long serialVersionUID = 1L;
	private ChatController chat;
	private NameFrame name;
	private JTextArea txtReceive;
	private JList lstGebruikers;
	private JButton btnDammen;
	private JButton btnSend;
	private JTextField txtSend;
	private JScrollPane scrReceive,scrGebruikers;
	private boolean uitdager = true;
	private String zijnNick;
	private boolean dammenActief;
	private DammenFrame f;
	
	/**
	* Auto-generated main method to display this JFrame
	*/
	
	
	public LobbyFrame() {
		super();
		
		initGUI();
		chat = new ChatController();
		chat.addChatListener(this);
		this.setVisible(false);
		name = new NameFrame();
		name.addNameListener(this);
		this.addWindowListener(new WindowListener()
		{
			public void windowActivated(WindowEvent w)
			{
		    }
		    public void windowClosed(WindowEvent w)
		    {
		    }
		    public void windowClosing(WindowEvent w)
		    {
		    	chat.exit();
		    	chat.send("(SC)");
		    	
		    	System.exit(0);
		    }
		    public void windowDeactivated(WindowEvent w)
		    {
		    }
		    public void windowDeiconified(WindowEvent w)
		    {
		    }
		    public void windowIconified(WindowEvent w) 
		    {
		    }
		    public void windowOpened(WindowEvent w)
		    {
		    }
		});
	}
	
	private void initGUI() {
		try {
			setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE);
			this.setVisible(true);
			getContentPane().setLayout(null);
			{
				txtReceive = new JTextArea();
				txtReceive.setBounds(5, 12, 200, 225);
				txtReceive.setEditable(false);
				
				scrReceive = new JScrollPane(txtReceive);
				scrReceive.setBounds(5, 7, 297, 225);
				scrReceive.setVisible(true);
				getContentPane().add(scrReceive);
			}
			{
				ListModel lstGebruikersModel = 
					new DefaultComboBoxModel();
				lstGebruikers = new JList();
				lstGebruikers.setModel(lstGebruikersModel);
				lstGebruikers.setBounds(308, 12, 79, 225);
				lstGebruikers.setSelectionMode(ListSelectionModel.SINGLE_SELECTION);
				scrGebruikers = new JScrollPane(lstGebruikers);
				scrGebruikers.setBounds(308, 7, 79, 195);
				scrGebruikers.setVisible(true);
				getContentPane().add(scrGebruikers);
			}
			{
				txtSend = new JTextField();
				getContentPane().add(txtSend);
				txtSend.setBounds(5, 245, 297, 21);
				txtSend.requestFocus();
				txtSend.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnSendActionPerformed(evt);
					}
				});
			}
			{
				btnSend = new JButton();
				getContentPane().add(btnSend);
				btnSend.setText("Verzenden");
				btnSend.setBounds(308, 245, 79, 21);
				btnSend.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnSendActionPerformed(evt);
					}
				});
			}
			{
				btnDammen = new JButton();
				getContentPane().add(btnDammen);
				btnDammen.setText("Dammen");
				btnDammen.setBounds(308, 209, 79, 21);
				btnDammen.addActionListener(new ActionListener() {
					public void actionPerformed(ActionEvent evt) {
						btnDammenActionPerformed(evt);
					}
				});
			}
			pack();
			setSize(400, 300);
			this.setResizable(false);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void btnSendActionPerformed(ActionEvent evt) 
	{
		chat.send(txtSend.getText());
		txtReceive.append(chat.getName() + ">>> " + txtSend.getText() + "\n");
		txtSend.setText("");
		txtSend.requestFocus();
	}
	private void btnDammenActionPerformed(ActionEvent evt) 
	{
		if(lstGebruikers.getSelectedIndex() != -1)
		{
			chat.send("(RD)" + lstGebruikers.getSelectedValue());
			zijnNick = lstGebruikers.getSelectedValue().toString();
		}
		else
			JOptionPane.showMessageDialog(null, "Geen tegenstander geselecteerd");
	}
	public void dataReceived(String data)
	{
		txtReceive.append(data + "\n");
	}
	public void clientConnected(String client)
	{
		txtReceive.setText(client + "\n");
	}
	public void clientDisconnected(String client)
	{
		txtReceive.setText(client + "\n");
	}
	public void nameGood()
	{
		name.nameGood();
	}
	public void nameBad()
	{
		name.nameBad();
	}
	public void setName(String n)
	{
		chat.setName(n);
	}
	public void sendName(String n)
	{
		chat.requestName(n);
	}
	public void listReceived(Object[] gebruikers)
	{
		lstGebruikers.setListData(gebruikers);
		this.setVisible(true);
	}
	public void challengeReceived(String name)
	{
		
		Object[] options = {"Accept",
                "Decline"};
		int n = JOptionPane.showOptionDialog(null,
				name + " challenge you to play Dammen",
				"Challenge",
				JOptionPane.YES_NO_OPTION,
				JOptionPane.QUESTION_MESSAGE,
				null,
				options,
				options[0]);

		if (n == JOptionPane.YES_OPTION)
		{
			zijnNick = name;
			uitdager = false;
			chat.send("(AD)" + name + ":accept");
			chat.send("(RI)"+name);
		}
		else
			chat.send("(AD)" + name + ":decline");
	}
	public void refuseChallenge(String name)
	{
		JOptionPane.showMessageDialog(null, name + " declines the challenge");
	}
	public void startDammen(String ip, String mijnNick)
	{
		if(dammenActief) return;
		try 
		{
			if (uitdager == true)
				f = new DammenFrame(1234, 4321,mijnNick, zijnNick, InetAddress.getByName(ip), true, this);
			else
				f = new DammenFrame(4321, 1234,mijnNick, zijnNick, InetAddress.getByName(ip), false, this);
			zijnNick = "";
			uitdager = true;
			
			dammenActief = true;
		} 
		catch (UnknownHostException e)
		{
			e.printStackTrace();
		}
	}
	
	public void setDammenEinde()
	{
		dammenActief = false;
	}
}
